from sanic import Blueprint
from sanic.response import json
from biz_code.component.comm import public_utils

admin_blu = Blueprint(__name__)


@admin_blu.route("/admin", methods=["POST"])
async def admin(request):
    ret = {"code": 0, "msg": "请求成功"}

    try:

        # jwt校验
        token_data = public_utils.jwt_decode(request.headers.get("token"))["data"]
        # 配置
        request.json["user_code"] = token_data["user_code"]
        # 参数校验
        params = public_utils.params_verify(request)

        func = request.app.func_map.get(params["act"])

        # 判断接口号处理方法是否存在
        if not func:
            raise Exception("func  not  fund")

        result = await func(request)

        ret["data"] = str(result)

    except Exception as error:
        print("error msg: ", error)
        ret["code"] = -1
        ret["msg"] = error.__str__()
    finally:
        print("aaa")
        return json(ret)
